home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / askto.arc / ASKTO.C next >
Encoding:
C/C++ Source or Header  |  1987-03-22  |  4.2 KB  |  157 lines

  1. /*
  2.  *  This software is provided for use AS-IS.  No warranty is made as
  3.  *  to its operation or correctness.
  4.  *
  5.  *  Written by David Dyer-Bennet
  6.  *  Terrabit Software
  7.  *  4242 Minnehaha Ave S
  8.  *  Minneapolis, MN 55406
  9.  *
  10.  *  Released to the public domain 22-Mar-87
  11.  *
  12.  *  UUCP: ...{amdahl,ihnp4,rutgers}!{meccts,dayton}!viper!ddb
  13.  *  UUCP: ...ihnp4!umn-cs!starfire!ddb
  14.  *  Fido: Sysop of Fido 14/341, The Terraboard, (612) 721-8967 3/12/24 24hrs
  15.  *  Telephone: (612) 721-8800 NOT 24 hrs!  More like noon to midnight
  16.  */
  17.  
  18. /*
  19.  *  This program asks a question at the console and reads an answer, returning
  20.  *  zero if the answer was "yes" and non-zero if the answer was "no".  So
  21.  *  far this is the same as ASK2, a standard pd utility.  The added filip
  22.  *  here is that if no answer is received after a specified or default
  23.  *  interval, a specified or default default answer is returned.  This is
  24.  *  useful in autoexec.bat for a system that often runs unattended (for
  25.  *  example, a bbs).  You can set it up so that, if nobody is around, it
  26.  *  brings the bbs up when power comes on after a failure.  However, if you
  27.  *  are present, you can tell it not to bring up the bbs.  Similarly with
  28.  *  multi-taskers like DoubleDos.
  29.  */
  30.  
  31. /*
  32.  *  Revision history:
  33.  *
  34.  *      Edit    Date        Who     Description
  35.  *
  36.  *  Version 1.0
  37.  *      1       22-Mar-87   DD-B    Initial creation
  38.  */
  39.  
  40.  
  41. #include <stdio.h>
  42. #include <conio.h>
  43. #include <time.h>
  44.  
  45. #define ANS_NO 1        /* status if no */
  46. #define ANS_YES 0        /* status if yes */
  47. #define DEFTIMEOUT 300        /* seconds */
  48. #define DEFANSWER ANS_NO    /* Default answer on timeout */
  49.  
  50. char szPrompt [128];        /* String to present to user */
  51. long tmWait;            /* Time to wait in seconds */
  52. long tmTerminate;        /* Time to terminate wait at if no answer */
  53. int bAnswer = DEFANSWER;    /* Not changed if timeout */
  54.  
  55. /*
  56.  *  Process the switch pointed to.  First character is the switch indicator.
  57.  */
  58.  
  59. void ProcSwitch (sp)
  60. char *sp;
  61. {
  62.     switch (tolower(*++sp)) {
  63.     case 'd':
  64.         tmWait = atol (++sp);
  65.     break;            /* case 'd' 'D' */
  66.     case 'a':
  67.         while (*++sp==' '||*sp=='\t')
  68.         ;        /* Skip leading white space */
  69.         if (tolower (*sp) == 'y')
  70.         bAnswer = ANS_YES;
  71.         else if (tolower (*sp) == 'n')
  72.         bAnswer = ANS_NO;
  73.         else
  74.         bomb ("-a option requires argument of y or n");
  75.     break;            /* case a */
  76.     }                /* switch *++sp */
  77. }                /* ProcSwitch () */
  78.  
  79. /*
  80.  *  Process another piece of the prompt string.  If enclosed in quotes, strip.
  81.  */
  82. ProcPrompt (pp)
  83. char *pp;
  84. {
  85.     int i;
  86.     i = strlen (pp) - 1;
  87.     if (*pp == '"' && *(pp+i) == '"') {
  88.     *(pp+i) = '\0';        /* Kill trailing quote */
  89.     pp++;            /* Kill leading quote */
  90.     }                /* if arg is quoted */
  91.     if (szPrompt[0] != 0) strcat (szPrompt, " ");
  92.     strcat (szPrompt, pp);
  93. }                /* ProcPrompt () */
  94.  
  95. /*
  96.  *  Terminate with extremem prejudice.
  97.  */
  98.  
  99. bomb (sp)
  100. char *sp;
  101. {
  102.     cputs ("? Ask error: ");
  103.     cputs (sp);
  104.     cputs ("\r\n");
  105.     cputs ("Usage: ask [-dseconds] [-adefaultanswer] prompt string\r\n");
  106.     exit (2);
  107. }                /* bomb () */
  108.  
  109. main (ac, av)
  110. int ac;
  111. char *av[];
  112. {
  113.     int ap, i;
  114.     char c;
  115.     
  116.     for (ap=1; ap<ac; ap++) {
  117.     if (*av[ap] == '-' || *av[ap] == '/') 
  118.         ProcSwitch (av[ap]);
  119.     else
  120.         ProcPrompt (av[ap]);
  121.     }                /* for ap */
  122.     i = strlen (szPrompt)-1;
  123.     if (szPrompt [i] != '?')
  124.     szPrompt [++i] = '?';
  125.     szPrompt [++i] = ' ';
  126.     cputs (szPrompt);
  127.     
  128.     time (&tmTerminate);    /* Get start time */
  129.     if (tmWait)            /* If delay (zero means forever) */
  130.     tmTerminate += tmWait;
  131.     else
  132.     tmTerminate = 0;
  133.     
  134.     while (!tmWait || time (NULL) < tmTerminate) {
  135.     if (kbhit()) {
  136.         c = getch ();    /* Get, no echo */
  137.         if (tolower (c) == 'y') {
  138.         putch (c);    /* echo */
  139.         bAnswer = ANS_YES;
  140.         break;
  141.         } else if (tolower (c) == 'n') {
  142.         putch (c);    /* echo */
  143.         bAnswer = ANS_NO;
  144.         break;
  145.         } else 
  146.         putch ('\007');    /* Beep for bad characters */
  147.     }            /* if kbhit () */
  148.     }                /* while !tmWait || time < tmTerminate */
  149.     cputs ("\r\n");
  150.     cputs ("[Ask: answering ");
  151.     cputs (bAnswer==ANS_YES?"YES":"NO");
  152.     cputs ("]\r\n");
  153.     exit (bAnswer);
  154. }                /* main () */
  155.  
  156. /* End of ask.c */
  157.